package com.sq.ai.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sq.ai.base.BaseController;
import com.sq.ai.config.AiMiddleConfig;
import com.sq.ai.entry.Channel;
import com.sq.ai.entry.ChannelConfig;
import com.sq.ai.entry.ChannelVo;
import com.sq.ai.mapper.ZtChannelMapper;
import com.sq.ai.pojo.SysConJob;
import com.sq.ai.pojo.ZtChannel;
import com.sq.ai.service.SysConJobService;
import com.sq.ai.service.ZtChannelService;
import com.sq.api.ai.ChannelService;
import com.sq.common.web.base.TableDataInfo;
import com.sq.common.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Li sir
 * @Date 2024/10/30
 */

@RestController
@RequestMapping("/api/algor2/job/")
public class SysConJob2Controller extends BaseController {

    @Autowired
    private SysConJobService sysConJobService;

    @Autowired
    private ZtChannelService ztChannelService;

    @Autowired
    private ZtChannelMapper ztChannelMapper;




    @PostMapping("start")
    public AjaxResult startConJob(@RequestBody SysConJob sysConJob){

        boolean b = sysConJobService.startConJob2(sysConJob);
        if (!b){
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @Autowired
    private AiMiddleConfig aiMiddleConfig;



    @PostMapping("startDan")
    public AjaxResult startDanConJob(@RequestBody ZtChannel ztChannel){
       //判断摄像头是否离线


       Channel channel=new Channel();
       SysConJob sysConJob=new SysConJob();
       ZtChannel ztChannel1=ztChannelService.getOne(new QueryWrapper<ZtChannel>().eq("channel_key", ztChannel.getChannelKey()));
       System.out.println(ztChannel1);
       String channelId = ztChannel1.getChannelId();
       String startAlgorUrl = aiMiddleConfig.getUrl(aiMiddleConfig.getStartDan())+channelId;
       System.out.println(startAlgorUrl);
       String res = HttpUtil.get(startAlgorUrl);

       return AjaxResult.success(res);
    }

    @PostMapping("updateJob")
    public AjaxResult updateConJob(@RequestBody SysConJob sysConJob){
        boolean b = sysConJobService.updateConJob(sysConJob);
        if (b){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @PostMapping("list")
    public TableDataInfo selectConJob(@RequestBody ZtChannel ztChannel){
        startPage(ztChannel.getPageNum(),ztChannel.getPageSize());
        List<ChannelVo> list = ztChannelService.selectZtChannel(ztChannel);
        return getDataTable(list);
    }

    @GetMapping("getOne/{jobId}")
    public AjaxResult getOne(@PathVariable("jobId") String jobId){
        SysConJob sysConJob = sysConJobService.selectConJobByJobId(jobId);
        return AjaxResult.success(sysConJob);
    }

    @GetMapping("del/{jobId}")
    public AjaxResult delJob(@PathVariable("jobId") String jobId){
        boolean b = ztChannelService.removeById(jobId);
        if (b){

            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //

}
